"""
Filesystem 模块 - 服务层
"""
from typing import AsyncIterator, List, Optional
from axiom_boot.di import service, autowired, conditional_on_setting
from axiom_boot.filesystem.interfaces import IFileSystem


@service(name="filesystemservice")
@conditional_on_setting(key="filesystem__enabled")
class FileSystemService:
    """
    文件系统服务层。

    这是与文件系统交互的主要入口点。它封装了底层的 IFileSystem 实现，
    为上层业务逻辑提供一个稳定、统一的接口。
    """

    def __init__(self, fs_impl: IFileSystem = autowired()):
        """
        注入主文件系统实现。
        DI 容器会根据配置自动选择 @primary 的实现 (LocalFileSystem 或 S3FileSystem)。
        """
        self._fs = fs_impl

    # --- 桶/目录级别操作 ---

    async def create_bucket(self, bucket_name: str, **kwargs) -> bool:
        return await self._fs.create_bucket(bucket_name, **kwargs)

    async def delete_bucket(self, bucket_name: str, **kwargs) -> bool:
        return await self._fs.delete_bucket(bucket_name, **kwargs)

    async def bucket_exists(self, bucket_name: str, **kwargs) -> bool:
        return await self._fs.bucket_exists(bucket_name, **kwargs)

    async def list_buckets(self) -> List[str]:
        return await self._fs.list_buckets()

    # --- 文件/对象级别操作 ---

    async def save(
        self,
        stream: AsyncIterator[bytes],
        path: str,
        bucket_name: Optional[str] = None,
        **kwargs
    ) -> str:
        return await self._fs.save(stream, path, bucket_name, **kwargs)

    async def read(
        self,
        path: str,
        bucket_name: Optional[str] = None,
        **kwargs
    ) -> AsyncIterator[bytes]:
        return self._fs.read(path, bucket_name, **kwargs)

    async def delete(
        self,
        path: str,
        bucket_name: Optional[str] = None,
        **kwargs
    ) -> bool:
        return await self._fs.delete(path, bucket_name, **kwargs)

    async def exists(
        self,
        path: str,
        bucket_name: Optional[str] = None,
        **kwargs
    ) -> bool:
        return await self._fs.exists(path, bucket_name, **kwargs)

    async def get_presigned_url(
        self,
        path: str,
        bucket_name: Optional[str] = None,
        expires_in: int = 3600,
        **kwargs
    ) -> str:
        return await self._fs.get_presigned_url(path, bucket_name, expires_in, **kwargs) 